package com.neusoft.common.util;

import java.util.*;

/**
 * 页码类
 * Created By YZ at Jun 30, 2010 11:27:52 AM
 * Class:Pagenation
 *
 */
public class Pagenation{

    /**
     * 记录数
     */
    private Integer recordCount;

    /**
     * 页面记录数
     */
    private Integer pageSize = 10;

    /**
     * 当前页码
     */
    private Integer currPage = 1;

    /**
     * 总页数
     */
    private Integer pageCount = 0;

    /**
     * 关键字类别（供查询使用）
     */
    private String[] keyTypes;

    /**
     * 关键字（供查询使用）
     */
    private String[] keyWords;

    /**
     * 排序字段
     */
    private String orderBy;

    /**
     * 升序/降序
     */
    private String sort;

    /**
     * 结果
     */
    private List<Object> items;

    public List<Object> getItems(){
        return items;
    }

    public void setItems(List<Object> items){
        this.items = items;
    }

    public Integer getCurrPage(){
        if(currPage < 1){
            currPage = 1;
        }
        return currPage;
    }

    public void setCurrPage(Integer currPage){
        this.currPage = currPage;
    }

    public String[] getKeyTypes(){
        return keyTypes;
    }

    public void setKeyTypes(String[] keyTypes){
        this.keyTypes = keyTypes;
    }

    public String[] getKeyWords(){
        return keyWords;
    }

    public void setKeyWords(String[] keyWords){
        this.keyWords = keyWords;
    }

    public Integer getPageCount(){
        return pageCount;
    }

    public void setPageCount(Integer pageCount){
        this.pageCount = pageCount;
    }

    public Integer getPageSize(){
        return pageSize;
    }

    public void setPageSize(Integer pageSize){
        this.pageSize = pageSize;
    }

    public Integer getRecordCount(){
        return recordCount;
    }

    public void setRecordCount(Integer cnt){
        this.recordCount = cnt;

        if(cnt.intValue() == 0){
            return;
        }
        pageCount = (int) ((cnt.intValue() + pageSize.intValue() - 1) / pageSize.intValue());

    }

    public String getOrderBy(){
        return orderBy;
    }

    public void setOrderBy(String orderBy){
        this.orderBy = orderBy;
    }

    public String getSort(){
        return sort;
    }

    public void setSort(String sort){
        this.sort = sort;
    }

    public int getRowBegin(){
        int begin = (currPage.intValue() - 1) * pageSize.intValue() ;
//        if(begin == 0){
//            begin = 1;
//        }

        return begin;
    }

    public int getRowEnd(){
        int end = currPage.intValue() * pageSize.intValue() + 1;
        return end;
    }

    @SuppressWarnings("unchecked")
    public static List emptyList(){
        return new ArrayList();
    }

    public int getPre(){
        int pre = currPage.intValue() - 1;
        if(pre < 1){
            pre = -1;
        }
        return pre;
    }

    public int getNext(){
        int next = currPage.intValue() + 1;
        if(next > pageCount){
            next = -1;
        }
        return next;
    }

    public int getPageBegin(){
        int pageBegin = currPage;
        int pageEnd = pageCount;

        if(pageEnd > 10){
            if(pageBegin > 5){
                pageBegin = pageBegin - 4;
                if(pageBegin + 8 < pageEnd){
                    pageEnd = pageBegin + 8;
                } else{
                    pageBegin = pageEnd - 9;
                }
            } else{
                pageBegin = 1;
                pageEnd = 9;
            }
        } else{
            pageBegin = 1;
        }
        if(pageEnd == 0){
            pageBegin = 0;
        }
        return pageBegin;
    }

    public int getPageEnd(){
        int pageBegin = currPage;
        int pageEnd = pageCount;

        if(pageEnd > 10){
            if(pageBegin > 5){
                pageBegin = pageBegin - 4;
                if(pageBegin + 8 < pageEnd){
                    pageEnd = pageBegin + 8;
                } else{
                    pageBegin = pageEnd - 9;
                }
            } else{
                pageBegin = 1;
                pageEnd = 9;
            }
        } else{
            pageBegin = 1;
        }
        return pageEnd;
    }

    /**
     * 设置page
     *
     * @param page
     */
    public static Pagenation setPage(Pagenation page){
        if(page != null){
            page.setItems(null);
        } else{
            page = new Pagenation();
        }
        return page;
    }
}
